home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Art / I / Imagic.cpt / External Functions / Average Source / myFunction.c < prev    next >
Text File  |  1992-04-03  |  4KB  |  149 lines

  1. /**************************************************************************************************
  2.  *                    myFunctions.c
  3.  *
  4.  *            This is the file where you (the programmer) will put your code.  The three functions
  5.  *        below are the skeletal parts of what you must fill in.  Just enter your code into each
  6.  *        corresponding function, compile, and link with the ImagicXFunction Library.  Then, you
  7.  *        are done.
  8.  *
  9.  *        See Documentation for further help.  All of your answers should be answered there.
  10.  *
  11.  *        For more complex and technical questions that the documentation could not answer,
  12.  * contact me, Brian Powell:
  13.  *
  14.  *                (E-Mail is the best way.  Preferably Internet)
  15.  *                Internet :   powellb@boulder.colorado.edu
  16.  *                AppleLink:   d3706
  17.  *
  18.  *                (If you have no other means...)                
  19.  *                Brian Powell
  20.  *                Colorado Center for Astrodynamics Research
  21.  *                University of Colorado at Boulder
  22.  *                Campus Box 431
  23.  *                Boulder, CO        80309
  24.  *                (303) 492-6677
  25.  *************************************************************************************************/
  26.  
  27. #include "XFunctions.h"
  28. #include <stdio.h>
  29.  
  30. /* Declare any functions you may create here.
  31.  */
  32. pascal void Border(Rect);
  33.  
  34. /* This is called whenever Imagic is beginning.  You set up your parameters here.  If there is
  35.  * anything you want to initialize, do that here.
  36.  */
  37. pascal OSErr Initialize(theParameters)
  38.     InitStruct *theParameters;
  39. {
  40.     OSErr theError = noErr;
  41.     
  42.     /* Insert Your Parameters Here.  If these parameters are fine with you, leave them, 
  43.      * otherwise set them up for your own needs.  This is also the default settings, so don't
  44.      * change it unless you need to. 
  45.      */
  46.      
  47.     theParameters->Filter = FALSE;
  48.     theParameters->NumOfImages = 3;
  49.     
  50.     /* Make sure that we are not goint to crash Imagic on startup with wrong calls. 
  51.      */
  52.     if (GetVersion() < EXTERNAL_FUNCTION_LIBRARY_VERSION_NUM) 
  53.         return (theError);
  54.         
  55.     /* Insert your initialization code here. 
  56.      */
  57.  
  58.     /* Let's go back to Imagic, giving it the parameters for our external module. */
  59.     return (theError);
  60. }
  61.  
  62. /* This function is called whenever the user selects your command from the menu.  This is the
  63.  * heart of your function.
  64.  */
  65. pascal OSErr ExecuteMenu()
  66. {
  67.     OSErr    theError=noErr;
  68.     ImageHandle image;
  69.     ImageList    images;
  70.     PixelValPtr    values1, values2, values3, newvals;
  71.     long        computed;
  72.     int            x, y, nx, ny, tx, ty, i, j;
  73.     int            number;
  74.     Point        topleft, botright;
  75.     short        proj;
  76.     char        string[256], dummy[256];
  77.     
  78.     
  79.     /* Ask the user for three images.
  80.      */
  81.      
  82.     if ((images = GetImageList(3,3,"Select 3 Images", &number)) == NIL) 
  83.         return (theError);
  84.     
  85.     /* Get the size of one image.
  86.      * NOTE: You should check to make sure that all of the images are of the same size;
  87.      * otherwise, things will get messed up.
  88.      */
  89.     GetImageSize((*images)[0], &x, &y);
  90.     GetImageSize((*images)[1], &nx, &ny);
  91.     GetImageSize((*images)[2], &tx, &ty);
  92.     if (x!=nx || x!=tx || y!=ny || y!=ty) 
  93.         return (theError);
  94.     
  95.     /* Get the geography of one of the images.  We'll just sit it the new one to the
  96.      * first one.
  97.      */
  98.     GetImageGeography((*images)[0], &topleft, &botright, &proj);
  99.     
  100.     /* Create an image
  101.      */
  102.     if ((image = NewBlankImage("Average", x, y, TRUE, &topleft, &botright, proj)) == NIL)
  103.         return (theError);
  104.     
  105.     /* Set up memory
  106.      */
  107.     newvals = (PixelValPtr)CreateNewPtr(x);
  108.  
  109.     /* Loop through grabbing information from each image and combine it.
  110.      */
  111.     if (BeginImageWork(image, FALSE)) {
  112.         if (!CreateProgressDialog("Computing Average", 0)) return;
  113.         for (i=0; i<y; i++) {
  114.             GetPixLineVal((*images)[0], &values1, i, 0);
  115.             GetPixLineVal((*images)[1], &values2, i, 0);
  116.             GetPixLineVal((*images)[2], &values3, i, 0);
  117.             for (j=0; j<x; j++) {
  118.                 computed = (long)((values1[j]+values2[j]+values3[j])/3);
  119.                 newvals[j] = (PixelVal)computed;
  120.             }
  121.             SetPixLineVal(image, newvals, i, 0);
  122.             if (!UpdateProgressDialog((double)i/(double)y*100)) break;
  123.         }
  124.     }
  125.     
  126.     /* Clear things up. 
  127.      */
  128.     DestroyPtr(newvals);
  129.     DisposProgressDialog();
  130.     EndImageWork(image);
  131.     return (theError);
  132. }
  133.  
  134. /* This function is called whenever Imagic quits.  If you need to clear anything up before it
  135.  * quits, do so here.
  136.  */
  137. pascal OSErr Exit()
  138. {
  139.     OSErr theError = noErr;
  140.     
  141.     /* Insert your shut-down code here.  Deallocate anything you may have left around, Do
  142.      * anything you may need to do as Imagic is quitting. 
  143.      */
  144.  
  145.     /* Let's let Imagic quit, returning an error that may have occured.
  146.      */
  147.     return (theError);
  148. }
  149.